<?php
	//daily process
	//include("../connect/connect_dsccmis.php");
	$sql = "select max(upddte) as upddte from gis_event_lastupdate";
	$rs=mysql_query($sql);
	$row=mysql_fetch_array($rs);
	$upddte=$row["upddte"];
	
	$sql = "drop table if exists temp_evnid";
	$rs=mysql_query($sql);
	
	//สร้างตารางชั่วคราวเก็บ evnid ที่ต้องลบออกจาก gis_event
	$sql="create table temp_evnid as select evnid from dscc.viewevent where upddte >= '$upddte'";
	//echo $sql;
	$rs=mysql_query($sql);
	
	//ลบเรคคอร์ดที่มีการปรับปรุงออกจาก gis_event
	$sql="delete from gis_event where evnid in (select evnid from temp_evnid)";
	$rs=mysql_query($sql);
	
	//เพิ่มเรคคอร์ดที่ลบออก โดยไปเอาข้อมูลมาจาก ตารางข้อมูลจริง	
    $sql="insert into gis_event  
select e1.evnid,e1.evndte,e1.evntme,left(e1.evnplcidgen,2) as prov,
concat(e1.evnplcidgen,e1.evnvllnogen) vill_code,'' as vill_name,
cdscc,e2.mis_evntypid,e1.evnnme,e1.evndes,e1.plcnmegen,e1.tarx,e1.tary,
null as lat,null as lng,e1.upddte from dscc.viewevent e1 left join  
(select e1.evnid,min(e3.mis_evntypid) as mis_evntypid from dscc.viewevent e1 
left join dscc.evnrelevntyp e2 on e1.evnid=e2.evnid
 left join convert_evntyp e3 on e2.evntypid=e3.evntypid group by e1.evnid)
 e2 on e1.evnid=e2.evnid left join dscc.TempEventAndCaseOfEvent e3
on e1.evnid=e3.evnid where e1.upddte >= '$upddte'";

	$rs=mysql_query($sql);
	//echo $sql;
	//mysql_close($c);
	
//delete all kml file of yesterday ---------------------------
//$tempY=date("Y");
//if($tempY<"2500"){
//	$tempY=(int)$tempY+543;
//}
//$datetime_yes= $tempY.date("md",strtotime("-1 day"));
//echo "datetime_yes=".$datetime_yes;
//foreach(glob('../gis_event'.$datetime_yes.'*.kml') as $file){
//foreach(glob('../kml/gis_event*.kml') as $file){
//     unlink($file);
//}
//-------------------------------------------------------------
echo "<br>$rs";
echo "<br>complete create gis_event";
?>